package com.ding.dailyup.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ding.dailyup.annotation.LoginUser;
import com.ding.dailyup.entity.JsonResult;
import com.ding.dailyup.entity.response.StockStrategyResp;
import com.ding.dailyup.entity.response.StrategyCategoryResp;
import com.ding.dailyup.entity.response.StrategyWithCategoryResp;
import com.ding.dailyup.model.StockStrategy;
import com.ding.dailyup.model.StrategyCategory;
import com.ding.dailyup.model.UserCollectStrategy;
import com.ding.dailyup.service.StockStrategyService;
import com.ding.dailyup.service.StrategyCategoryService;
import com.ding.dailyup.service.UserCollectStrategyService;
import com.ding.dailyup.util.UserTokenUtil;
import com.google.common.collect.Lists;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/strategy/category")
@Api(tags = "策略分类")
public class CategoryController {
	@Autowired
	private StrategyCategoryService strategyCategoryService;
	@Autowired
	private StockStrategyService stockStrategyService;

	private UserCollectStrategyService userCollectStrategyService;

	@ApiOperation(value = "策略&分类")
	@GetMapping(value = "/list")
	public JsonResult<List<StrategyWithCategoryResp>> getStrategyCategory(
			@LoginUser Long userId) {
		List<Long> collectStrategyIds = Lists.newArrayList();
		List<StockStrategyResp> userCollectStrategyList = Lists.newArrayList();

		// 处理收藏状态
		if (UserTokenUtil.isLogin(userId)) {
			List<UserCollectStrategy> userCollectStrategies = userCollectStrategyService
					.listUserCollectStrategies(userId);
			collectStrategyIds = userCollectStrategies.stream()
					.map(UserCollectStrategy::getStrategyId)
					.collect(Collectors.toList());
			userCollectStrategyList = userCollectStrategies.stream()
					.map(userCollectStrategy -> {
						StockStrategyResp stockStrategyResp = new StockStrategyResp();
						BeanUtil.copyProperties(userCollectStrategy,
								stockStrategyResp);
						return stockStrategyResp;
					}).collect(Collectors.toList());
		} else {
			List<StockStrategy> defaultStrategies = stockStrategyService
					.listDefaultStrategies();
			collectStrategyIds = defaultStrategies.stream()
					.map(StockStrategy::getId).collect(Collectors.toList());
			userCollectStrategyList = defaultStrategies.stream()
					.map(stockStrategy -> {
						StockStrategyResp stockStrategyResp = new StockStrategyResp();
						BeanUtil.copyProperties(stockStrategy,
								stockStrategyResp);
						return stockStrategyResp;
					}).collect(Collectors.toList());
		}

		List<StrategyCategory> strategyCategories = strategyCategoryService
				.listStrategyCategories();
		List<StrategyWithCategoryResp> rs = Lists.newArrayList();
		for (StrategyCategory strategyCategory : strategyCategories) {
			StrategyWithCategoryResp strategyWithCategoryResp = new StrategyWithCategoryResp();
			StrategyCategoryResp strategyCategoryResp = new StrategyCategoryResp();
			BeanUtils.copyProperties(strategyCategory, strategyCategoryResp);
			List<Long> finalCollectStrategyIds = collectStrategyIds;
			List<StockStrategyResp> stockStrategiesReps = stockStrategyService
					.findByCategoryId(strategyCategory.getCategoryId()).stream()
					.map(stockStrategy -> {
						StockStrategyResp stockStrategyResp = new StockStrategyResp();
						BeanUtils.copyProperties(stockStrategy,
								stockStrategyResp);
						stockStrategyResp.setCollected(finalCollectStrategyIds
								.contains(stockStrategyResp.getId()));
						return stockStrategyResp;
					}).collect(Collectors.toList());
			strategyWithCategoryResp
					.setStockStrategyRespList(stockStrategiesReps);
			strategyWithCategoryResp.setStrategyCategory(strategyCategoryResp);
			rs.add(strategyWithCategoryResp);
		}

		return JsonResult.succResult(rs);
	}

}
